//École de Technologie Supérieure (ÉTS)
//LOG720 - Laboratoire 3  - Intégration de Spring + Hibernate
//Date de remise: 2011-12-13
//Auteurs:
//Jonathan Aubuchon (AUBJ18038704)
//Jeff Grenier (GREJ02098703)
//Francis Paré (PARF21038708)

package springapp.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import springapp.service.AjouterInfractionAuDossierCommand;
import springapp.service.DossierManager;
import springapp.service.InfractionManager;

public class ConsulterDossierController extends SimpleFormController {

	
	public ModelAndView onSubmit(Object command) throws ServletException {
		
		AjouterInfractionAuDossierCommand ajouterInfractionAuDossierCommand = (AjouterInfractionAuDossierCommand)command;
		
		boolean isSuccess= DossierManager.getInstance().ajouterInfractionADossier(ajouterInfractionAuDossierCommand.getDossierID(), 
				ajouterInfractionAuDossierCommand.getInfractionID());

		System.out.println("success: " + isSuccess);
		Map<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("infractions", InfractionManager.getInstance().getInfractions());
		
		myModel.put("dossier", DossierManager.getInstance().getDossiers());
		
		String successView = "Dossier.htm?dossierID="+ajouterInfractionAuDossierCommand.getDossierID();
        return new ModelAndView(new RedirectView(successView));
        
    }
	

	protected Map referenceData(HttpServletRequest request) throws Exception
	{
		Map<String, Object> myModel = new HashMap<String, Object>();
		Integer id = Integer.parseInt(request.getParameter("dossierID"));
		
		myModel.put("infractions", InfractionManager.getInstance().getInfractions());
		myModel.put("dossier", DossierManager.getInstance().getDossier(id));
		return myModel;
	}

	
    protected Object formBackingObject(HttpServletRequest request)
            throws ServletException {
    	
    	Integer dossierID = Integer.parseInt(request.getParameter("dossierID"));
    	AjouterInfractionAuDossierCommand ajouterInfractionAuDossierCommand = new AjouterInfractionAuDossierCommand();   	
    	ajouterInfractionAuDossierCommand.setDossierID(dossierID);
    	return ajouterInfractionAuDossierCommand;
    }
	
	

}
